#ifndef EVENT_POOL_H
#define EVENT_POOL_H

#include "Acceptor.h"
#include <sys/epoll.h>
#include <iostream>
#include <vector>
#include <map>
#include <memory>
#include <utility>
#include <functional>

class TcpConnect;

using std::cout;
using std::endl;
using std::function;
using std::map;
using std::shared_ptr;
using std::vector;

class EventPool
{
    using TcpConnectPtr = shared_ptr<TcpConnect>;
    using callback = function<void(TcpConnectPtr)>;

private:
    int _fd;
    int _epfd;
    Acceptor &_acceptor;
    vector<epoll_event> _evts; // 就绪队列
    map<int, TcpConnectPtr> _tcps;
    bool _isLooping;

    ThreadPool _pool{};

    callback _handleMessageCallback;
    callback _handleClosedCallback;
    callback _handleConnectCallback;

private:
    void init();
    bool isClosed(int);

public:
    EventPool(Acceptor &);
    ~EventPool();
    void loop();
    void unloop();
    void epollWait();
    void addNewEpollCtl(int);
    void addNewTcpConnect(int);
    void handleMessage(int);
    void delEpollCtl(int);
    void delTcpConnect(TcpConnectPtr);

    void setMessageCallback(callback &&cb);
    void setClosedCallback(callback &&cb);
    void setConnectCallback(callback &&cb);
};

#endif